1 /*
2 Copyright (c)
2014 Andrew Jones, Dario Seyb
3  Based
on 'Spriter2Unity' python code by Malhavok
4
5 Permission
is hereby granted, free of charge, to any person obtaining a copy
6 of
this software and associated documentation files (the "Software"), to deal
7 in
the Software without restriction, including without limitation the rights
8 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 copies of the Software, and to permit persons to whom the Software
is
10 furnished to
do so, subject to the following conditions:
11
12 The above copyright notice and
this permission notice shall be included in
13 all copies or substantial portions of the Software.
14
15 THE SOFTWARE IS PROVIDED
"AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21 THE SOFTWARE.
22 */

23 using
UnityEngine;
24 using
UnityEditor;
25 using
System;
26 using
System.Collections.Generic;
27 using
System.Linq;
28 using
System.Text;
29 using
Spriter2Unity.Runtime;
30
31 namespace
Assets.ThirdParty.Spriter2Unity.Editor.Unity
32 {
33     
//Name clash between Spriter and Unity - use the Spriter version by default
34     
using Animation = Assets.ThirdParty.Spriter2Unity.Editor.Spriter.SpriterAnimation;
35     
using Assets.ThirdParty.Spriter2Unity.Editor.Spriter;
36
37     
public class PrefabBuilder
38     {
39         
public GameObject MakePrefab(Entity entity, GameObject root, string spriteFolder)
40         {
41             
//Set the name (in case it changed)
42             root.name = entity.Name;
43
44             
if(!root.GetComponent<SpriterEntity>())
45             {
46                 root.AddComponent<SpriterEntity>();
47             }
48
49             
//Build the GameObject hierarchy
50             
foreach (var animation in entity.Animations)
51             {
52                 MakePrefab(animation, root, spriteFolder);
53             }
54             
return root;
55         }
56
57         
private void MakePrefab(Animation animation, GameObject root, string spriteFolder)
58         {
59             
foreach(var mainKey in animation.MainlineKeys)
60             {
61                 MakePrefab(mainKey, root, spriteFolder);
62             }
63         }
64
65         
private struct RefParentInfo
66         {
67             
public Ref Ref;
68             
public GameObject Root;
69         }
70
71         
private void MakePrefab(MainlineKey mainKey, GameObject root, string spriteFolder)
72         {
73             
var rootInfo = mainKey.GetChildren(null).Select(child => new RefParentInfo { Ref = child, Root = root });
74             Stack<RefParentInfo> toProcess =
new Stack<RefParentInfo>(rootInfo);
75             
while(toProcess.Count > 0)
76             {
77                 
var next = toProcess.Pop();
78                 
var go = MakePrefab(next.Ref, next.Root, spriteFolder);
79                 
foreach(var child in mainKey.GetChildren(next.Ref))
80                 {
81                     toProcess.Push(
new RefParentInfo{Ref = child, Root = go});
82                 }
83             }
84         }
85
86         
private GameObject MakePrefab(Ref childRef, GameObject root, string spriteFolder)
87         {
88             
var timeline = childRef.Referenced.Timeline;
89             
var transform = root.transform.Find(timeline.Name);
90             GameObject go;
91             
if(transform == null)
92             {
93                 go = MakeGameObject(childRef, root, spriteFolder);
94             }
95             
else
96             {
97                 go = transform.gameObject;
98             }
99             
return go;
100         }
101
102         
private GameObject MakeGameObject(Ref childRef, GameObject parent, string spriteFolder)
103         {
104             TimelineKey key = childRef.Referenced;
105             GameObject go =
new GameObject(key.Timeline.Name);
106             
if (parent != null)
107             {
108                 go.transform.parent = parent.transform;
109             }
110
111             
//Any objects that show up only after t=0 begin inactive
112             
if(key.Time_Ms > 0)
113             {
114                 go.SetActive(
false);
115             }
116
117             
var spriteKey = key as SpriteTimelineKey;
118             
if (spriteKey != null)
119             {
120                 
//Set initial sprite information
121                 
var sprite = go.AddComponent<SpriteRenderer>();
122                 
//Hack to be able to animate the sortingOrder property
123                 go.AddComponent<SortingOrderUpdate>();
124                 sprite.sprite = AssetUtils.GetSpriteAtPath(spriteKey.File.Name, spriteFolder);
125             }
126
127             SetTransform(childRef, go.transform);
128
129             
return go;
130         }
131
132         
private void SetTransform(Ref childRef, Transform transform)
133         {
134             Vector3 localPosition;
135             Vector3 localScale;
136             Vector3 localEulerAngles;
137
138             PrefabUtils.BakeTransforms(childRef,
out localPosition, out localEulerAngles, out localScale);
139             transform.localPosition = localPosition;
140             transform.localScale = localScale;
141             transform.localEulerAngles = localEulerAngles;
142         }
143     }
144 }


Name clash between Spriter and Unity - use the Spriter version by default

Set the name (in case it changed)

Build the GameObject hierarchy

Any objects that show up only after t=0 begin inactive

Set initial sprite information

Hack to be able to animate the sortingOrder property




Trò chơi đua xe động vật trong UNITY Engine 114.809 lượt xem

Gõ tìm kiếm nhanh...